<?php

/*
  Action - Reaction	 3$

  Discard a Treasure from your hand +1 coin per $
  When an opponent plays an Atack card you may reveal this card from your hand. Give the attacker 1 Treasure card from your hand and ignore the attack.
  The attacker either gains the Treasure or trashes it (his choise)

 */

class Paycheck extends Card implements iAction, iKingdom, iReactingCard, iReaction, iAddiction {

    public function __construct() {
        parent::__construct("Paycheck", 2);
        $this->undoPlay = false;
        $this->undoBuy = true;

        $this->reactConditions = new ReactionConditions();
        $this->reactConditions->action = array(Attack);
        $this->reactConditions->actor = array(SomeoneElse);
        $this->reactConditions->cardLocation = array(PlayHand);
        $this->reactConditions->limit = ForEachCard;
    }

    public function getDescription() {
        return array(
            "Discard a Treasure from your hand +1 coin per $",
            "When an opponent plays an Attack card you may reveal this card from your hand. Give the attacker 1 Treasure card from your hand and ignore the attack.",
            "The attacker either gains the Treasure or trashes it (his choise)");
    }

    public function play(Game $game, $isRealCard) {

        $playHand = $game->getPlayHand();
        $treasures = array();
        foreach ($playHand as $card) {
            if ($card instanceof iTreasure) {
                $treasures[] = $card;
            }
        }

        if (sizeof($treasures) > 0) {

            $treasures = getUniqueCards($treasures);
            if (sizeof($treasures) > 1) {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $treasures, 1, 1, null, "Choose treasure to discard");
                $treasure_card = $output[0];
            } else {
                $treasure_card = $treasures[0];
            }

            $treasure_card = $game->moveCardFromPileToPile($treasure_card, PlayHand, DiscardPile);
            $coins = $treasure_card->getPrice($game)->getCoins();
            $game->addMoney($coins);
            ScreenDisplay::addScreenMessage($game, true, "discard  " . nicePrint($treasure_card) . " and get $coins$", "discards " . ScreenDisplay::$LMSG . " and gets $coins$");
        } else {
            ScreenDisplay::addScreenMessage($game, true, "discard nothing and get 0$", "discards nothing and gets 0$");
        }

        $game->cardActionFinished();
        return true;
    }

    public function isReactRelevant(Game $game, $card, $name) {
        $treasures = array();
        $cards = $game->getPlayHand($name);
        foreach ($cards as $card) {
            if ($card instanceof iTreasure) {
                $treasures[] = $card;
            }
        }
        if (sizeof($treasures) == 0)
            return false;
        return true;
    }

    public function getConditions() {
        return $this->reactConditions;
    }

    public function react(Game $game, $card, $name) {
        ScreenDisplay::addScreenMessage($game, true, "reveal " . nicePrint($this), "reveals " . nicePrint($this), $name);
        $treasures = array();
        $cards = $game->getPlayHand($name);
        foreach ($cards as $pcard) {
            if ($pcard instanceof iTreasure) {
                $treasures[] = $pcard;
            }
        }

        if (sizeof($treasures) > 0) {
            $treasures = getUniqueCards($treasures);
            if (sizeof($treasures) > 1) {
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $treasures, 1, 1, null, "Choose treasure to pass to $name", $name);
                $treasure_card = $output[0];
            } else {
                $treasure_card = $treasures[0];
            }

            ScreenDisplay::addScreenMessage($game, true, "pass  " . nicePrint($treasure_card), "passes " . ScreenDisplay::$LMSG, $name);
            $treasure_card = $game->moveCardFromPileToPile($treasure_card, PlayHand, Trash, $name);
            $oldPhase = $game->getPhase($name);
            $game->setPhase(Phase_Sleep, $name);
            $attacking_player = $game->getCurrentPlayerName();
            $options = array(new Option(1, "Yes"), new Option(2, "No"));
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Would you like to gain " . $treasure_card->getValue(), $attacking_player);
            $answer = $output[0];
            if ($answer == 'Yes') {
                $game->moveCardFromPileToPile($treasure_card, Trash, DiscardPile, $attacking_player);
                ScreenDisplay::addScreenMessage($game, false, " and $attacking_player gets it", " and $attacking_player gets it", $name);
            } else {
                ScreenDisplay::addScreenMessage($game, false, " and $attacking_player trashes it", " and $attacking_player trashes it", $name);
            }
            $game->protectFromAttack($name);
            $game->setPhase($oldPhase, $name);
        } else {
            ScreenDisplay::addScreenMessage($game, true, "pass nothing", "passes nothing", $name);
        }
        $game->setTempCard($card);
        return true;
    }

}

?>